package com.fyales.android.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 密码加密工具类
 * @author fyales
 * @since 15/7/24.
 */
public class SecurityUtil {

    /**
     * 将明文密码加密,返回
     * @param pass 明文密码
     * @return 加密后的密码
     */
    public static String md5(String pass){
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(pass.getBytes());
            pass = byte2hex(digest.digest());
        }catch(NoSuchAlgorithmException e){
            e.printStackTrace();
        }
        return pass;
    }

    /**
     * 二进制转十进制
     * @param bytes byte数组
     * @return 字符串
     */
    public static String byte2hex(byte[] bytes)
    {
        StringBuffer sb = new StringBuffer();
        for (byte b : bytes) {
            sb.append(String.format("%02x", b & 0xff));
        }
        return sb.toString();
    }
}

